package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.SelectBelongsAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.SelectBelongsBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择所属项目---页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectBelongsActivity extends BaseActivity implements View.OnClickListener,HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private ACache mCache;

    private SelectBelongsAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_belongs);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择所属项目");

        //{"currentUserId":当前员工employeeId," serviceProviderCode ":当前公司编码 ,"ownerCode":选择的发起企业Code}
        ServiceLoginBean serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        String companyCode = getIntent().getStringExtra("companyCode");

        if (serviceLoginBean != null) {
            if (!TextUtils.isEmpty(companyCode)){

                String jsonBean = "{currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",ownerCode:\"" +companyCode+ "\"}";
                serviceSelectNewUnitHttp(jsonBean);

            }
        }
    }

    //服务商工单所属项目
    private void serviceSelectNewUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectBelongsActivity.this);
        hp.setUrl(Api.SERVICE_QUERYCONTRACT_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void setListDate(List<SelectBelongsBean.Rows> mList){

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SelectBelongsAdapter(SelectBelongsActivity.this,mList);
        adapter.setOnItemClickListener(new SelectBelongsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String contractName,String contractId) {
                Intent intent = new Intent();
                intent.putExtra("contractName",contractName);
                mCache.put("contractId",contractId);
                setResult(RESULT_OK, intent);
                adapter.setSelection(position);
                adapter.notifyDataSetChanged();
                SelectBelongsActivity.this.finish();
            }
        });

        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId){
            case 1:
                SelectBelongsBean selectBelongsBean = new Gson().fromJson(result,SelectBelongsBean.class);
                if (selectBelongsBean.getMessage().getCode() == 200){
                    setListDate(selectBelongsBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
